package wrs.hcs.functions;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;

public class AdminConsole_Pair {
	
	static
	AdminConsole ac = new AdminConsole();
	Miscellaneous m = new Miscellaneous();
	
	public void admin(WebDriver driver, String pin) throws Exception{
		
		ac.wellness_search_it(driver, pin, "", "");
		ac.get_status_and_click(driver, pin, true);
		String mainWindow = ac.window_main_focus(driver);
		
		//PATIENT 1 FORM
		driver.switchTo().frame(driver.findElement(By.name("patient1")));
		ac.update_pair_online_in_admin(driver);
		ac.select_doctor_single(driver, "pair", mainWindow);
		driver.switchTo().window(mainWindow);
		ac.submit_form(driver);
		ac.close_error_popup_after_submit_form(driver);
		for(int z=0; z<26; z++){
			driver.findElement(By.xpath("//input[@class='btnPrimary']")).sendKeys(Keys.ARROW_UP);	
		}
		driver.findElement(By.xpath("//a[@id='patientTwoTab']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.switchTo().frame(driver.findElement(By.name("patient2")));
		ac.select_room_pair(driver, 2);
		ac.select_patient_care_officer(driver);
		ac.select_doctor_single(driver, "pair", mainWindow);
		driver.switchTo().window(mainWindow);
		for(int z=0; z<2; z++){
			driver.findElement(By.xpath("//input[@class='btnPrimary']")).sendKeys(Keys.ARROW_DOWN);	
		}
		ac.submit_form(driver);
		driver.switchTo().window(mainWindow);
		//ac.close_popup_after_submit_form(driver);
		//ac.select_back_button(driver);

		Webdriver d = new Webdriver();
		driver = d.startUp();
		Login log = new Login();
		log.admin(driver); 	
		ac.wellness_search_it(driver, pin, "", "");
		ac.get_status_and_click(driver, pin, false);
		String text = m.set_dropdown_to_hcs("online");
		ac.select_dropdown_to_hcs(driver, text, "pair");
		ac.display_message_to_hcs(driver);
	}
	
	public void addon(WebDriver driver, String pin) throws Exception{
		
		ac.wellness_search_it(driver, pin, "", "");
		ac.get_status_and_click(driver, pin, true);
		String mainWindow = ac.window_main_focus(driver);
		
		//PATIENT 1 FORM
		driver.switchTo().frame(driver.findElement(By.name("patient1")));
		ac.create_addon(driver, "online", "pair");
		ac.update_pair_online_in_admin(driver);
		ac.select_doctor_single(driver, "pair", mainWindow);
		driver.switchTo().window(mainWindow);
		ac.submit_form(driver);
		ac.close_error_popup_after_submit_form(driver);
		for(int z=0; z<26; z++){
			driver.findElement(By.xpath("//input[@class='btnPrimary']")).sendKeys(Keys.ARROW_UP);	
		}
		driver.findElement(By.xpath("//a[@id='patientTwoTab']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.switchTo().frame(driver.findElement(By.name("patient2")));
		ac.select_room_pair(driver, 2);
		ac.select_patient_care_officer(driver);
		ac.create_addon(driver, "online", "single");
		ac.select_doctor_single(driver, "pair", mainWindow);
		driver.switchTo().window(mainWindow);
		for(int z=0; z<2; z++){
			driver.findElement(By.xpath("//input[@class='btnPrimary']")).sendKeys(Keys.ARROW_DOWN);	
		}
		ac.submit_form(driver);
		Thread.sleep(30000);
		Login log = new Login();
		log.admin(driver);
		ac.wellness_search_it(driver, pin, "", "");
		ac.get_status_and_click(driver, pin, false);

		//INSERT VALIDATION OF TABLE HERE
		ac.pair_table_validation(driver, "single_online", "inpatient");
		String text = m.set_dropdown_to_hcs("online");
		ac.select_dropdown_to_hcs(driver, text, "pair");
		ac.display_message_to_hcs(driver);
	}
	
	public void pair(WebDriver driver, String pin1, String pin2) throws Exception{
		ac.add_reservation(driver, "pair");
		ac.search_pin_on_admin_console(driver, pin1, pin2, "pair");
		String mainWindow = driver.getWindowHandle();
		driver.switchTo().frame(driver.findElement(By.name("patient1")));
		ac.input_the_reservation_type_of_patient(driver, "phone");
		ac.input_the_contact_number_of_patient(driver);
		ac.input_the_package_of_patient(driver, "onlineoverweight");
		ac.input_the_date_of_admission_of_patient(driver,"");
		ac.input_the_time_of_admission_of_patient(driver);
		ac.select_unit_single(driver, "admin");
		ac.input_the_room_type_of_patient(driver, "private");
		ac.input_the_booker_of_patient(driver);
		ac.select_patient_care_officer(driver);
		ac.input_the_responsible_for_patient(driver, "self");
		for(int y=0; y<8; y++){driver.findElement(By.xpath("//label[contains(.,'With Letter of Authorization (LOA)')]")).sendKeys(Keys.ARROW_UP);}
		ac.select_doctor_single(driver, "pair", mainWindow);
		ac.submit_form(driver);
		ac.close_error_popup_after_submit_form(driver);
		for(int z=0; z<26; z++){
			driver.findElement(By.xpath("//input[@class='btnPrimary']")).sendKeys(Keys.ARROW_UP);	
		}
		driver.findElement(By.xpath("//a[@id='patientTwoTab']")).click();
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		driver.switchTo().frame(driver.findElement(By.name("patient2")));
		ac.input_the_contact_number_of_patient(driver);
		ac.input_the_package_of_patient(driver, "onlineoverweight");
		ac.input_the_booker_of_patient(driver);
		ac.select_patient_care_officer(driver);
		ac.input_the_responsible_for_patient(driver, "self");
		for(int y=0; y<8; y++){driver.findElement(By.xpath("//label[contains(.,'With Letter of Authorization (LOA)')]")).sendKeys(Keys.ARROW_UP);}
		ac.select_doctor_single(driver, "pair", mainWindow);
		ac.submit_form(driver);
		System.out.println(driver.findElement(By.xpath("//p[@id='errorMessage']")).getText());
		ac.close_popup_after_save_new_patient(driver);
		ac.wellness_search_it(driver, pin1, "", "");
		ac.get_status_and_click(driver, pin1, false);
		String text = m.set_dropdown_to_hcs("queue");
		ac.select_dropdown_to_hcs(driver, text, "pair");
		Thread.sleep(20000);
		ac.display_message_to_hcs(driver);
		ac.wellness_search_it(driver, pin1, "", "");
		ac.get_status_and_click(driver, "", false);
		AdminConsole_Tracker act = new AdminConsole_Tracker();
		act.goto_result_tracker(driver);
		act.tracker(driver, pin1);
		act.tracker(driver, pin2);
	}
	
}
